package com.yuanli.cosmeticmirror.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cgfay.filter.glfilter.resource.FilterHelper;
import com.cgfay.filter.glfilter.resource.bean.ResourceData;
import com.kongzue.dialog.v3.CustomDialog;
import com.yuanli.cosmeticmirror.R;
import com.yuanli.cosmeticmirror.base.BaseActivity;
import com.yuanli.cosmeticmirror.base.MyApp;
import com.yuanli.cosmeticmirror.event.MainEvent;
import com.yuanli.cosmeticmirror.view.adapter.FilterAdapter;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

import static com.yuanli.cosmeticmirror.base.Constant.ACTION_START_FILTER_SWITCHING;

/**
 * 滤镜
 *
 * @author Administrator
 */
public class FilterActivity extends BaseActivity {
    @BindView(R.id.filter_back)
    ImageView filterBack;
    @BindView(R.id.filter_rv)
    RecyclerView filterRv;
    private FilterAdapter adapter;

    private boolean isCheck;

    @Override
    protected int getContentView() {
        return R.layout.activity_filter;
    }

    @Override
    protected void init(Bundle bundle) {

    }

    @Override
    protected void initView() {
        adapter = new FilterAdapter();
        filterRv.setLayoutManager(new GridLayoutManager(this, 4));
        filterRv.setAdapter(adapter);
        adapter.setOnItemClickListener((adapter, view, position) -> {
            ResourceData data = (ResourceData) adapter.getItem(position);
            if (data != null) {
                if (position > 11 && !MyApp.IsVIP) {
                    getCustomDialogPaly();
                    return;
                }
                //                isCheck = ;
                if (!data.isCheck) {
                    MyApp.POSITION = position;
                    data.isCheck = true;
                    EventBus.getDefault().post(new MainEvent<>(ACTION_START_FILTER_SWITCHING, position));
                } else {
                    data.isCheck = false;
                    EventBus.getDefault().post(new MainEvent<>(ACTION_START_FILTER_SWITCHING, 0));
                }
                this.adapter.setPosition(position);
                //                } else {
                //                    EventBus.getDefault().post(new MainEvent<>(ACTION_START_FILTER_SWITCHING, 0));
                //                }
            }
        });
    }

    /**
     * 滤镜界面  提示需要会员
     *
     * @return
     */
    private CustomDialog getCustomDialogPaly() {
        return CustomDialog.show(FilterActivity.this, R.layout.dialog_member_warm_2, (dialog, v) -> {
            TextView btnOk = v.findViewById(R.id.member_enter);
            TextView btncancel = v.findViewById(R.id.member_cancel);
            btnOk.setOnClickListener(v1 -> {
                startActivity(new Intent(FilterActivity.this, MemberActivity.class));
                finish();
                dialog.doDismiss();
            });
            btncancel.setOnClickListener(v1 -> {
                dialog.doDismiss();
            });
        }).setAlign(CustomDialog.ALIGN.DEFAULT);
    }

    @Override
    protected void initData() {
        adapter.setNewInstance(FilterHelper.getFilterList());
    }

    @Override
    protected void destroy() {
        //        mOnFilterChangeListener = null;
    }

    @OnClick(R.id.filter_back)
    public void onViewClicked() {
        finish();
    }

}
